package com.iteye.weimingtom.japcoffee;

import com.example.hellojni.HelloJni;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class SearchableDictionary extends Activity {
	private final static boolean D = true;
	private final static String TAG = "SearchableDictionary";
	
	static AssetManager assetManager;
	
    private EditText mEditText1;
    private ListView mListView;
    private TextView mTextView;
    private Button button1;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        String str = new HelloJni().stringFromJNI();
        if (D) {
        	Log.e(TAG, "str : " + str);
        }
        assetManager = this.getAssets();
        boolean result = HelloJni.createAssetAudioPlayer(assetManager, "dict.dat");
        
        initSearch();

        mEditText1 = (EditText) findViewById(R.id.editText1);
        mListView = (ListView) findViewById(R.id.list);
        mTextView = (TextView) this.findViewById(R.id.textView1);
        button1 = (Button) this.findViewById(R.id.button1);

        button1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String query = mEditText1.getText().toString();
	            showResults(query);
			}
        });
    }

    private void showResults(String query) {
        Cursor cursor = search(query);
        if (cursor == null) {
            mTextView.setText("没有搜索结果: " + query);
        } else {
            int count = cursor.getCount();
            String countString = "\"" + query + "\"的搜索结果: " + count;
            mTextView.setText(countString);
            String[] from = new String[] { DictionaryDatabase.KEY_WORD, DictionaryDatabase.KEY_DEFINITION };
            int[] to = new int[] { R.id.word, R.id.definition };
            SimpleCursorAdapter words = new SimpleCursorAdapter(this, R.layout.result, cursor, from, to);
            mListView.setAdapter(words);
            if (D) {
            	Log.e(TAG, "words: " + words.getCount());
            }
            mListView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                	Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class);
                    wordIntent.putExtra(WordActivity.EXTRA_ROWID, String.valueOf(id));
                    startActivity(wordIntent);
               	}
            });
        }
    }
    
    private DictionaryDatabase mDictionary;
    private void initSearch() {
    	mDictionary = new DictionaryDatabase(this);
    }
    private Cursor search(String query) {
        query = query.toLowerCase();
        String[] columns = new String[] {
            BaseColumns._ID,
            DictionaryDatabase.KEY_WORD,
            DictionaryDatabase.KEY_DEFINITION};

        return mDictionary.getWordMatches(query, columns);
      }
}
